<?php
namespace Admin\Controller;
use Admin\Controller\CommonController;
/**
 * 领料顾客或车辆控制器
 */
class CustomerController extends CommonController{
	/**
	 * 领料顾客或车辆列表
	 */
	public function index(){
		$pageNum=I('pageNum',1,intval);//第几页
		$numPerPage=I('numPerPage',10,intval);//每页显示条数
		//获取排序的字段及排序方式
		$orderField=$_POST['_order']?$_POST['_order']:'id';
		$orderDirection=$_POST['_sort']?$_POST['_sort']:'asc';
		//组合排序字符串
		$orders=($orderField.' '.$orderDirection);


                //检索条件
        if($_POST['search_where']){
            $this->search_where=$_POST['search_where'];
        }
        $where=$this->_search();
        //在where条件中增加未删除的判断条件
        $where['del']=array('eq','0');

		$db=D('CustomerRelation');
		//获取总数据条数
		$count=$db->where($where)->count();
		$this->assign('totalCount',$count);//数据的总条数
		$this->assign('numPerPage',$numPerPage);//每页显示多少条
		$this->assign('pageNumShown',6);//页标数字多少个
		$this->assign('currentPage',$pageNum);//当前是第几页
		$page= new \Think\Page($count,$numPerPage);
		$this->list=$db->limit($numPerPage)->Page($pageNum)->order($orders)->relation(true)->where($where)->select();
		$this->assign('customer_addId',128);//添加
        $this->assign('customer_editId',129);//编辑
        $this->assign('customer_delId',130);//删除
        $this->assign('customer_statusId',131);//状态修改
        $this->display();
	}

    public function add(){
        if(!empty($_POST)){
            $db=D('customer');
            if(!$db->create()) {
                $this->error($db->getError());
            }else{
                // 写入帐号数据
                if($result   =   $db->add()) {
                    $this->success('添加成功');
                }else{
                    $this->error('添加失败');
                }
            }
        }else{
        	$db=M('company');
    		$this->company=$db->select();	
        	$this->display();
        }
    }
	

    public function edit(){
        if(!empty($_POST)){
            $db   = D("customer");
            if(!$db->create()) {
                $this->error($db->getError());
            }else{
                // 修改帐号数据
                if($result=$db->save()) {
                    $this->success('修改成功！');
                }else{
                    $this->error('修改失败！');
                }
            }
        }else{
        	$id = $_GET['id'];
        	$db=M('company');
    		$this->company=$db->select();
        	$db1=M('customer');
        	$field=array('id','name','type','contacts','phone','remark','status','company_id','customer_code');
    		$this->customer=$db1->field($field)->find($id);
    		$this->display();
        }

    }
    public function lookup(){
        $pageNum=I('pageNum',1,intval);//第几页
        $numPerPage=I('numPerPage',10,intval);//每页显示条数
        //获取排序的字段及排序方式
        $orderField=$_POST['_order']?$_POST['_order']:'id';
        $orderDirection=$_POST['_sort']?$_POST['_sort']:'asc';
        //组合排序字符串
        $orders=($orderField.' '.$orderDirection);

        //检索条件
        if($_POST['search_where']){
            $this->search_where=$_POST['search_where'];
        }
        $where=$this->_search();
        //在where条件中增加未删除的判断条件
        $where['del']=array('eq','0');
        //在where条件中增加状态可用的判断条件
        $where['status']=array('eq','1');
        $db=M('customer');
        //获取总数据条数
        $count=$db->where($where)->count();
        $this->assign('totalCount',$count);//数据的总条数
        $this->assign('numPerPage',$numPerPage);//每页显示多少条
        $this->assign('pageNumShown',6);//页标数字多少个
        $this->assign('currentPage',$pageNum);//当前是第几页
        $page= new \Think\Page($count,$numPerPage);
        $this->list=$db->limit($numPerPage)->Page($pageNum)->order($orders)->where($where)->select();
        $this->display();
    }



}